﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC22BO
    /// </summary>
    public class VYC22BO:DBConnectionBO
    {
        public List<ModelVYC22> GetAllList()
        {
            var list = new List<ModelVYC22>();
            const string query = "SELECT [Id],[IdAlbumAnh],[TieuDe],[AnhDaiDien],[TomTat],[NoiDung],[LuotXem],[NgayCapNhat],[TrangThai] FROM [VYC-22]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC22();

                model.Id = Convert.ToInt32(dataTable.Rows[i][0]);
                model.IdAlbumAnh = dataTable.Rows[i][1].ToString();
                model.TieuDe = dataTable.Rows[i][2].ToString();
                model.AnhDaiDien = dataTable.Rows[i][3].ToString();
                model.TomTat = dataTable.Rows[i][4].ToString();
                model.TomTat = dataTable.Rows[i][5].ToString();
                model.NoiDung = dataTable.Rows[i][6].ToString();
                model.LuotXem = Convert.ToInt32(dataTable.Rows[i][7]);
                model.NgayCapNhat = (DateTime) dataTable.Rows[i][8];
                model.TrangThai = Convert.ToInt32(dataTable.Rows[i][9]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC22 model)
        {
            var query = "INSERT INTO [VYC-22]([IdAlbumAnh],[TieuDe],[AnhDaiDien],[TomTat],[NoiDung],[LuotXem],[NgayCapNhat]," +
                        "[TrangThai]) VALUES (@idalbumanh,@tieude, @anhdaidien, @tomtat, @noidung,@luotxem, GETDATE()," +
                        "@trangthai)";
            var sqlCommand = new SqlCommand(query, connection);

            // sqlCommand.Parameters.Add("@id", SqlDbType.VarChar).Value = model.Id;
            sqlCommand.Parameters.Add("@idalbumanh", SqlDbType.VarChar).Value = model.IdAlbumAnh;
            sqlCommand.Parameters.Add("@tieude", SqlDbType.NVarChar).Value = model.TieuDe;
            sqlCommand.Parameters.Add("@anhdaidien", SqlDbType.VarChar).Value = model.AnhDaiDien;
            sqlCommand.Parameters.Add("@tomtat", SqlDbType.NVarChar).Value = model.TomTat;
            sqlCommand.Parameters.Add("@noidung", SqlDbType.NText).Value = model.NoiDung;
            sqlCommand.Parameters.Add("@luotxem", SqlDbType.Int).Value = model.LuotXem;
            sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = model.TrangThai;

            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC22 model)
        {
            var query = "UPDATE [VYC-22] SET " +
                        "[IdAlbumAnh] = " + model.IdAlbumAnh + "', " +
                        "[TieuDe] = N'" + model.TieuDe + "', " +
                        "[AnhDaiDien] = '" + model.AnhDaiDien + "', " +
                        "[TomTat] = N'" + model.TomTat + "', " +
                        "[NoiDung] = N'" + model.NoiDung + ", " +
                        "[LuotXem] = " + model.LuotXem + ", " +
                        "[NgayCapNhat] = GETDATE(), [TrangThai] = " + model.TrangThai +
                        " WHERE Id = " + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC22 model)
        {
            var query = "DELETE FROM [VYC-22] " +
                        " WHERE Id = " + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC22> GetById(string id)
        {
            var list = new List<ModelVYC22>();
            var query = "SELECT [Id],[IdAlbumAnh],[TieuDe],[AnhDaiDien],[TomTat],[NoiDung],[LuotXem],[NgayCapNhat],[TrangThai] FROM [VYC-22] WHERE Id like '" + id + "'";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC22();

                model.Id = Convert.ToInt32(dataTable.Rows[i][0].ToString());
                model.IdAlbumAnh = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.TieuDe = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.AnhDaiDien = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.TomTat = (dataTable.Rows[i][4] == DBNull.Value) ? string.Empty : dataTable.Rows[i][4].ToString();
                model.TomTat = (dataTable.Rows[i][5] == DBNull.Value) ? string.Empty : dataTable.Rows[i][5].ToString();
                model.NoiDung = (dataTable.Rows[i][6] == DBNull.Value) ? string.Empty : dataTable.Rows[i][6].ToString();
                model.LuotXem = (dataTable.Rows[i][7] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][7]);
                model.NgayCapNhat = (DateTime)dataTable.Rows[i][8];
                model.TrangThai = Convert.ToInt32(dataTable.Rows[i][9]);

                list.Add(model);
            }
            return list;
        }


        public List<ModelVYC22> GetLimitAlbumForModule(int trangthai, string idalbumanh)
        {
            var list = new List<ModelVYC22>();
            try
            {
                var query = "SELECT * FROM [VYC-22] WHERE [IdAlbumAnh] like @idalbumanh and TrangThai = @trangthai Order by NgayCapNhat DESC";
                var sqlCommand = new SqlCommand(query, connection);

                sqlCommand.Parameters.Add("@idalbumanh", SqlDbType.VarChar).Value = idalbumanh;
                sqlCommand.Parameters.Add("@trangthai", SqlDbType.Bit).Value = trangthai;

                var reader = sqlCommand.ExecuteReader();

                while (reader.Read())
                {
                    var model = new ModelVYC22();

                    model.Id = Convert.ToInt32(reader["Id"]);
                    model.TieuDe = reader["TieuDe"].ToString();
                    model.AnhDaiDien = reader["AnhDaiDien"].ToString();
                    model.IdAlbumAnh = reader["IdAlbumAnh"].ToString();
                    model.TomTat = reader["TomTat"].ToString();
                    model.NgayCapNhat = (DateTime)reader["NgayCapNhat"];
                    model.TrangThai = Convert.ToInt32(reader["TrangThai"]);

                    list.Add(model);
                }
                return list;
            }
            catch (Exception)
            {
                CloseConnection();
                throw;
            }
            finally
            {
                CloseConnection();
            }

        }
    }
}